package com.example.careycai.playactivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity {

    private Button btn1,btn2;
    private EditText et1,et2;
    private CheckBox cb;
    private SharedPreferences spf;
    private SharedPreferences.Editor editor;
    private MyDatabaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        spf= PreferenceManager.getDefaultSharedPreferences(this);
        btn1=(Button)findViewById(R.id.button);
        btn2=(Button)findViewById(R.id.button2);
        et1=(EditText)findViewById(R.id.editText);
        et2=(EditText)findViewById(R.id.editText2);
        cb=(CheckBox)findViewById(R.id.checkBox2);
        dbHelper=new MyDatabaseHelper(this,"Play.db",null,1);
        boolean isRemember=spf.getBoolean("remember_password", false);
        if(isRemember){
            String accout=spf.getString("account","");
            String password=spf.getString("password","");
            et1.setText(accout);
            et2.setText(password);
            cb.setChecked(true);
        }
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String sql1 = "select id,name,password,time from userinfo";
                Cursor cursor = db.rawQuery(sql1, null);
                int t = 0;
                while (cursor.moveToNext()) {
                    do {
                        String username1 = cursor.getString(cursor.getColumnIndex("name"));
                        String password1 = cursor.getString(cursor.getColumnIndex("password"));
                        String account = et1.getText().toString();
                        String password = et2.getText().toString();
                        if ((account.equals(username1)) && (password.equals(password1))) {
                            editor = spf.edit();
                            if (cb.isChecked()) {
                                editor.putBoolean("remember_password", true);
                                editor.putString("account", account);
                                editor.putString("password", password);
                            } else {
                                editor.clear();
                            }
                            editor.commit();
                            t = 1;
                            Intent intent = new Intent(Login.this, MainActivity.class);
                            startActivity(intent);
                           finish();
                        }
                    } while (cursor.moveToNext());
                }
                if (t == 0) {
                    Toast.makeText(Login.this, "登录失败", Toast.LENGTH_LONG).show();
                }
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Login.this,Register.class);
                startActivity(intent);
            }
        });
    }


}
